<@override name="contest_content">
<#assign type="Private<sup>${contest.type}</sup>">
<#if contest.type==0>
<#assign type="Public">
</#if>
<h3 class="text-center">${type} Contest ${contest.cid!}: ${contest.title!}</h3>
<div class="row text-center">
  <div class="span6 offset3">
    <#if (contest.type > 1)>
    <div class="contest-admin">
      <a href="contest/adminUser/${contest.cid!}" class="btn btn-primary">Manage Users</a>
    </div>
    </#if>
    <div class="well">
      <span>Start time</span>: <span class="time">${contest.startDateTime!}</span> 
      <span>End time</span>: <span class="time">${contest.endDateTime!}</span><br>
      <span>Current System Time</span>: <span class="time" id="current">${contest.startDateTime!}</span> 
      <span>Contest Status</span>: <span class="status ${status!}">${status!}</span>
    </div>
  </div>
</div>
<div class="row text-center">
  <div class="span4 offset4">
    <p>${contest.description!}</p>
  </div>
</div>

<div class="row">
  <div class="span8 offset2">
    <table id="problem-list" class="table table-hover table-condensed">
      <thead>
        <tr>
          <th>ID</th>
          <th>Problem</th>
          <th>Title</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <#if contestProblems??>
        <#list contestProblems as Problem>
        <tr class="problem" pid="${Problem.pid!}">
          <td><i class="icon-resize-vertical"></i><span class="id"></span></td>
          <td class="pid">
            <a href="problem/show/${Problem.pid!}">${Problem.pid!}</a>
          </td>
          <td class="title"><a href="contest/problem/${contest.cid!}-${Problem.id!}">${Problem.title!}</a></td>
          <td class="admin"><#if serverTime<contest.startTime><button class="btn btn-danger remove" pid="${Problem.pid!}">Remove</button></#if></td>
        </tr>
        </#list>
        </#if>
        <#if serverTime<contest.endTime>
        <tr>
          <form class="form-inline" id="addProblem" action="api/contest/addProblem" method="post">
          <td><input type="hidden" name="cid" value="${contest.cid!}"><span class="id"></span></td>
          <td><input type="number" name="pid" class="input-small" min="1000" placeholder="Problem ID" required></td>
          <td><input type="text" name="title" class="input-xlarge" placeholder="Title" required></td>
          <td>
            <button type="submit" class="btn btn-success">Add</button> <#if serverTime<contest.startTime><a class="btn btn-info hidden" id="reorder">Save</a></#if></td>
          </form>
        </tr>
        </#if>
      </tbody>
    </table>
  </div>
</div>

</@override>

<@override name="styles">
  <link href="assets/jquery.artDialog/skins/twitter.css" rel="stylesheet" type="text/css">
</@override>
<@override name="scripts">
<#if oj_style != "slate">
<link href="assets/tablecloth/css/tablecloth.css" rel="stylesheet" type="text/css">
<script src="assets/tablecloth/js/jquery.metadata.js"></script>
<script src="assets/tablecloth/js/jquery.tablecloth.js"></script>
</#if>

<script src="assets/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="assets/jquery.artDialog/jquery.artDialog.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    var current_time = ${serverTime!}*1000;
    function updateTime() {
      current_time+=1000;
      $("#current").html(new Date(current_time).format("yyyy-MM-dd hh:mm:ss"));
    }
    setInterval(updateTime, 1000);
    
    function getProblemTitle() {
      var pid = this.value;
      if (pid >= 1000)
      {
        $.get("api/problem/getField", { name: "title", pid: this.value },
          function(data) {
            if (data.result)
            {
              $("input[name='title']").val(data.result);
            }
            else
            {
              $("input[name='title']").val('');
            }
          });
      }
      else
      {
        $("input[name='title']").val('');
      }
    }
    $("input[name='pid']").change(getProblemTitle).keyup(getProblemTitle);
    
    // Return a helper with preserved width of cells
    var fixHelper = function(e, ui) {
      ui.children().each(function() {
        $(this).width($(this).width());
      });
      return ui;
    };
    
    function updateId() {
      $("#problem-list tbody tr").each(function(i, e) {
          var newId = String.fromCharCode(65+i);
          $(e).children().children('.id').html(newId);
        });
    }
    updateId();
    <#if serverTime<contest.startTime>
    $("#problem-list tbody").sortable({
      helper: fixHelper,
      connectWith: "tr",
      stop: function(e, info) {
        info.item.after(info.item.parents('tr'));
        $('#reorder').removeClass('hidden');
        updateId();
      }
    }).disableSelection();
    </#if>
  
    <#if oj_style != "slate">
    $("#problem-list").tablecloth({
      theme:"stats",
      condensed:true,
      sortable:false,
      striped:true,
      clean:true
    });
    </#if>

    $(function() {
      $('#addProblem').submit(function() {
        var that = $(this);
        var cid = $('input[name=cid]').val();
        var pid = $('input[name=pid]').val();
        var title = $('input[name=title]').val();
        $.post($(this).attr("action"), $('#addProblem').serialize(),
          function(data) {
            switch(data.result)
            {
              case -1:
                $.dialog({
                  content: 'Db error.',
                  time: 1.5
                });
                break;
              case -2:
                $.dialog({
                  content: 'Too many problems in this contest.',
                  time: 1.5
                });
                break;
              case -3:
                $.dialog({
                  content: 'Duplicate problems for this contest.',
                  time: 1.5
                });
                break;
              case -4:
                $.dialog({
                  content: 'This problem does not exist.',
                  time: 1.5
                });
                break;
              case -5:
                $.dialog({
                  content: 'Contest already finished.',
                  time: 1.5
                });
                break;
              default:
                var html = '<tr class="problem" pid="' + pid + '">' +
                          '<td><i class="icon-resize-vertical"></i><span class="id"></span></td>' +
                          '<td class="pid">' +
                          '<a href="problem/show/' + pid + '" target="_blank">' + pid + '</a>' +
                          '</td>' +
                          '<td class="title"><a href="contest/problem/' + cid + '-' + String.fromCharCode(65+data.result) + '">' + title + '</a></td>' +
                          '<td class="admin"><#if serverTime<contest.startTime><button class="btn btn-danger remove" pid="' + pid + '">Remove</button></#if></td>' +
                          '</tr>';
                that.parent().before(html);
                updateId();
                break;
            }
          });
        return false;
      });
      $('#reorder').click(function() {
        var data = {cid:${contest.cid!}, pid:''};
        $("#problem-list tbody tr.problem").each(function(i, e) {
          data.pid += $(e).attr('pid')+',';
        });
        data.pid = data.pid.substring(0, data.pid.length-1);
        $.post('api/contest/reorderProblem', data, function(data) {
          if (data.success) {
              $.dialog({
                content: 'Reorder problem success.',
                time: 1
              });
            }
            else
            {
              $.dialog({
                content: data.result,
                time: 1.5
              });
            }
          });
      });
      $('table').on('click', '.remove', function() {
        var that = $(this);
        $.dialog({
          title: 'Remove problem from contest',
          content: 'Are you sure?',
          cancelVal: 'Cancel',
          cancel: true,
          okVal: 'Remove',
          ok: function() {
            $.post('api/contest/removeProblem', {cid:${contest.cid!}, pid:that.attr('pid')}, function(data) {
              if (data.success) {
                  that.parent().parent().remove();
                  updateId();
                  $.dialog({
                    content: 'Problem removed.',
                    time: 1
                  });
                }
                else
                {
                  $.dialog({
                    content: data.result,
                    time: 1.5
                  });
                }
            });
            this.close();
            return false;
          }
        });
      });
    });
  });
</script>
</@override>
<@extends name="_layout.html" />
